package com.evildog.workspace.system.application.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.dependencies.mybatis.base.TableLinkService;
import com.evildog.workspace.dependencies.mybatis.object.BaseStatusDTO;
import com.evildog.workspace.dependencies.mybatis.object.LinkIdDTO;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.BaseStatusValidate;
import com.evildog.workspace.system.application.bo.ComponentPermissionListBO;
import com.evildog.workspace.system.application.convert.ComponentPermissionConvert;
import com.evildog.workspace.system.application.dto.ComponentPermissionDTO;
import com.evildog.workspace.system.application.dto.ComponentPermissionListDTO;
import com.evildog.workspace.system.application.entity.ComponentApiPermissionLink;
import com.evildog.workspace.system.application.entity.ComponentPermission;
import com.evildog.workspace.system.application.entity.ComponentPermissionListDO;
import com.evildog.workspace.system.application.mapper.ComponentPermissionMapper;
import com.evildog.workspace.system.application.service.ComponentApiPermissionLinkService;
import com.evildog.workspace.system.application.service.ComponentPermissionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
@Slf4j
public class ComponentPermissionServiceImpl extends ServiceImpl<ComponentPermissionMapper, ComponentPermission> implements ComponentPermissionService {

    private ComponentApiPermissionLinkService componentApiPermissionLinkService;
    private TableLinkService tableLinkService;

    @Override
    public List<ComponentPermissionListBO> getList(List<QueryConditionDTO> queryConditions, ComponentPermissionListDTO pageDTO) {
        List<ComponentPermissionListDO> list = this.baseMapper.getList(
                queryConditions,
                pageDTO);
        return ComponentPermissionConvert.INSTANCE.convertBOList(list);
    }

    @Override
    public boolean save(ComponentPermissionDTO dto){
        ComponentPermission newComponentPermission = ComponentPermissionConvert.INSTANCE.convert(dto);
        return save(newComponentPermission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateApiPermissions(Long id, LinkIdDTO linkIdDTO) {
        ComponentPermission componentPermission = getById(id);
        Assert.notNull(componentPermission, "componentPermission is null");

        List<Long> linkIds = linkIdDTO.getLinkIds();

        tableLinkService.removeByLinkId(componentApiPermissionLinkService, ComponentApiPermissionLink::getComponentPermissionId, id);

        if (!linkIds.isEmpty()) {
            List<ComponentApiPermissionLink> links = new ArrayList<>();
            linkIds.forEach(linkId ->
                    links.add(new ComponentApiPermissionLink()
                            .setComponentPermissionId(id)
                            .setApiPermissionId(linkId)
                    ));

            componentApiPermissionLinkService.saveBatch(links);
        }

        return true;
    }

    @Override
    public boolean updateStatus(Long id, BaseStatusDTO baseStatus) {
        ComponentPermission componentPermission = getById(id);
        Assert.notNull(componentPermission, "componentPermission is null!");

        Integer status = baseStatus.getStatus();
        BaseStatusValidate.checkStatusValid(status);

        ComponentPermission updateDO = new ComponentPermission()
                .setId(id)
                .setStatus(status);
        return updateById(updateDO);
    }

    @Override
    public boolean updateById(Long id, ComponentPermissionDTO dto){
        ComponentPermission oldComponentPermission = getById(id);
        Assert.notNull(oldComponentPermission, "componentPermission is null!");
        ComponentPermission newComponentPermission = ComponentPermissionConvert.INSTANCE.convert(dto);
        newComponentPermission.setId(id);
        return updateById(newComponentPermission);
    }
}
